package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicOtherInwhDAO;
import com.cssy.yd.logic.dao.LogicOtherInwhItemBatchDAO;
import com.cssy.yd.logic.dao.LogicOtherInwhItemDAO;
import com.cssy.yd.logic.dao.LogicPriceDAO;
import com.cssy.yd.logic.entity.LogicOtherInwhDO;
import com.cssy.yd.logic.entity.LogicOtherInwhItemBatchDO;
import com.cssy.yd.logic.entity.LogicOtherInwhItemDO;
import com.cssy.yd.logic.entity.LogicPriceDO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.service.ILogicOtherInwhService;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Service("logicOtherInwhService")
public class LogicOtherInwhServiceImpl implements ILogicOtherInwhService{

	@Resource(name = "logicOtherInwhDAO")
	private LogicOtherInwhDAO logicOtherInwhDAO;
	@Resource(name = "logicOtherInwhItemDAO")
	private LogicOtherInwhItemDAO logicOtherInwhItemDAO;
	
	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicPriceDAO")
	private LogicPriceDAO logicPriceDAO;
	
	@Resource(name="logicOtherInwhItemBatchDAO")
	private LogicOtherInwhItemBatchDAO logicOtherInwhItemBatchDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicOtherInwhDO>  list= logicOtherInwhDAO.find(scs);
		rs.setRecordCount(logicOtherInwhDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		LogicOtherInwhDO basic = new LogicOtherInwhDO();
		basic.setDutime(new Date());
		basic.setDuuser(user.getName());
		basic.setId(id);
		basic.setRemoved(1);
		logicOtherInwhItemDAO.deleteByPid(id);
		return logicOtherInwhDAO.updateLogicOtherInwhDO(basic);
	}

	@Override
	public Integer update(LogicOtherInwhDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicOtherInwhDAO.updateLogicOtherInwhDO(t);
	}

	@Override
	public LogicOtherInwhDO save(LogicOtherInwhDO t, User user)
			throws SyBaseException {
		Date date=new Date();
		t.setDcruser(user.getName());
		t.setDctime(date);
		logicOtherInwhDAO.insertLogicOtherInwhDO(t);
		return t;
	}

	@Override
	public LogicOtherInwhDO findListById(String id) throws SyBaseException {
		return logicOtherInwhDAO.findLogicOtherInwhDOByPrimaryKey(id);
	}

	@Override
	public void coSave(LogicOtherInwhDO curAP, HttpServletRequest request)
			throws SyBaseException, SyCodeException {
		try{
			String json = request.getParameter("addressInfos");
			List<LogicOtherInwhItemDO> axxlist=JSONArray.parseArray(json, LogicOtherInwhItemDO.class);
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				int rt=0;
				rt=this.update(curAP, SessionUtil.getUser(request));
				if(0==rt){
					throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
				}
				if(curAP.getBillStatus() != 3){
					List<LogicOtherInwhItemDO> list= logicOtherInwhItemDAO.findByPid(curAP.getId());
					for (int i = 0; i < list.size(); i++) {
						logicOtherInwhItemBatchDAO.deleteByotherInwhItemId(list.get(i).getId());
					}
					logicOtherInwhItemDAO.deleteByPid(curAP.getId());
					for (LogicOtherInwhItemDO lii : axxlist){
						String id = IDCodeTypeUtil.getId();
						lii.setId(id);
						lii.setOtherInwhId(curAP.getId());
						logicOtherInwhItemDAO.insertLogicOtherInwhItemDO(lii);
						if(lii.getItemBatch() != null && !"".equals(lii.getItemBatch())){
							List<LogicOtherInwhItemBatchDO> itemBatchDOs=JSONArray.parseArray(lii.getItemBatch(), LogicOtherInwhItemBatchDO.class);
							if(itemBatchDOs != null && itemBatchDOs.size() > 0){
								for (int i = 0; i < itemBatchDOs.size(); i++) {
									itemBatchDOs.get(i).setId(IDCodeTypeUtil.getId());
									itemBatchDOs.get(i).setOtherInwhItemId(id);
									logicOtherInwhItemBatchDAO.insertLogicOtherInwhItemBatchDO(itemBatchDOs.get(i));
								}
							}
						}
					}
				}
				if (curAP.getBillStatus() == 3){
					LogicOtherInwhDO loi = logicOtherInwhDAO.findLogicOtherInwhDOByPrimaryKey(curAP.getId());
					for (LogicOtherInwhItemDO lii : axxlist){
						if (curAP.getBillStatus() == 3){
							if("Y".equals(lii.getOpenBatch()) && lii.getItemBatch() != null){
								if(lii.getItemBatch() != null && !"".equals(lii.getItemBatch())){
									List<LogicOtherInwhItemBatchDO> itemBatchDOs=JSONArray.parseArray(lii.getItemBatch(), LogicOtherInwhItemBatchDO.class);
									if(itemBatchDOs != null && itemBatchDOs.size() > 0){
										for (int i = 0; i < itemBatchDOs.size(); i++) {
											LogicWhAffDO lw = new LogicWhAffDO();
											lw.setBillCode(loi.getOtherCode());
											lw.setBatch(itemBatchDOs.get(i).getBatch());
											lw.setBillType("CO");
											lw.setCode(lii.getCode());
											lw.setDcruser(SessionUtil.getUser(request).getName());
											lw.setDctime(new Date());
											lw.setName(lii.getName());
											lw.setQuantity(itemBatchDOs.get(i).getQuantity());
											lw.setSpec(lii.getSpec());
											lw.setStatus(itemBatchDOs.get(i).getStatus());
											lw.setBrand(lii.getBrand());
											lw.setHubName(curAP.getWhName());
											lw.setWhCode(loi.getWhCode());
											lw.setWhname(loi.getWhName());
											lw.setUnit(lii.getUnit());
											lw.setJunit(lii.getJunit());
											lw.setJquantity(itemBatchDOs.get(i).getJquantity());
											lw.setBzq(lii.getBzq());
											lw.setYxq(lii.getYxq());
											lw.setScrq(lii.getScrq());
											if(lii.getPrice()==null||lii.getPrice()<=0){
												SearchConditions scs = new SearchConditions();
												scs.addCondition("wh_code", loi.getWhCode(),SearchCondition.OP_EQUAL);
												scs.addCondition("code", lii.getCode(),SearchCondition.OP_EQUAL);
												List<LogicPriceDO> lp=logicPriceDAO.find(scs);
												if(lp.size()>0){
													if(lp.get(0).getSumprice()!=null&&lp.get(0).getSumprice()>0){
														lw.setPrice(lp.get(0).getSumprice());
													}else{
														throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格为0！入库失败！请填写物料价格");
													}
												}else{
													throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格！入库失败！请填写物料价格");
												}
											}else{
												lw.setPrice(lii.getPrice());
											}
											logicWhAffStockService.updateWhAffstockQuantity(lw, SessionUtil.getUser(request),false);
										}
									}
								}
							}else{
								LogicWhAffDO lw = new LogicWhAffDO();
								lw.setBillCode(loi.getOtherCode());
								lw.setBatch("NoBatch");
								lw.setBillType("CO");
								lw.setCode(lii.getCode());
								lw.setDcruser(SessionUtil.getUser(request).getName());
								lw.setDctime(new Date());
								lw.setName(lii.getName());
								lw.setQuantity(lii.getQuantity());
								lw.setSpec(lii.getSpec());
								lw.setStatus(lii.getStatus());
								lw.setBrand(lii.getBrand());
								lw.setHubName(curAP.getWhName());
								lw.setWhCode(loi.getWhCode());
								lw.setWhname(loi.getWhName());
								lw.setUnit(lii.getUnit());
								lw.setJunit(lii.getJunit());
								lw.setJquantity(lii.getJquantity());
								lw.setBzq(lii.getBzq());
								lw.setYxq(lii.getYxq());
								lw.setScrq(lii.getScrq());
								if(lii.getPrice()==null||lii.getPrice()<=0){
									SearchConditions scs = new SearchConditions();
									scs.addCondition("wh_code", loi.getWhCode(),SearchCondition.OP_EQUAL);
									scs.addCondition("code", lii.getCode(),SearchCondition.OP_EQUAL);
									List<LogicPriceDO> lp=logicPriceDAO.find(scs);
									if(lp.size()>0){
										if(lp.get(0).getSumprice()!=null&&lp.get(0).getSumprice()>0){
											lw.setPrice(lp.get(0).getSumprice());
										}else{
											throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格为0！入库失败！请填写物料价格");
										}
									}else{
										throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格！入库失败！请填写物料价格");
									}
								}else{
									lw.setPrice(lii.getPrice());
								}
								logicWhAffStockService.updateWhAffstockQuantity(lw, SessionUtil.getUser(request),false);
							}
						}
					}
				}
			}else{
					String pcode=syCodeService.getCode("CO","CO",SyCode.DATE_YYMM,5);
					curAP.setOtherCode(pcode);
					String id = IDCodeTypeUtil.getId();
					curAP.setId(id);
					LogicOtherInwhDO cdo=this.save(curAP, SessionUtil.getUser(request));
					for (LogicOtherInwhItemDO lii : axxlist){
						String itemId = IDCodeTypeUtil.getId();
						lii.setId(itemId);
						lii.setOtherInwhId(id);
						logicOtherInwhItemDAO.insertLogicOtherInwhItemDO(lii);
						if(lii.getItemBatch() != null && !"".equals(lii.getItemBatch())){
							List<LogicOtherInwhItemBatchDO> itemBatchDOs=JSONArray.parseArray(lii.getItemBatch(), LogicOtherInwhItemBatchDO.class);
							if(itemBatchDOs != null && itemBatchDOs.size() > 0){
								for (int i = 0; i < itemBatchDOs.size(); i++) {
									itemBatchDOs.get(i).setId(IDCodeTypeUtil.getId());
									itemBatchDOs.get(i).setOtherInwhItemId(itemId);
									logicOtherInwhItemBatchDAO.insertLogicOtherInwhItemBatchDO(itemBatchDOs.get(i));
								}
							}
						}
					}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}
}
